﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ZHDJ.Data.BLL;

namespace ZHDJ.Data.Models
{
    public class partyvideoprobe : EntityBase
    {
        #region 字段属性

        /// <summary>
        /// 标题
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 电信地址Url
        /// </summary>
        public string AddressTelecom { get; set; }

        /// <summary>
        /// 电信端口
        /// </summary>
        public string PortTelecom { get; set; }
        /// <summary>
        /// 联通地址
        /// </summary>
        public string AddressUnicon { get; set; }
        /// <summary>
        /// 联通端口
        /// </summary>
        public string PortUnicon { get; set; }
        /// <summary>
        /// 移动地址
        /// </summary>
        public string AddressMobile { get; set; }
        /// <summary>
        /// 移动端口
        /// </summary>
        public string PortMobile { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string LinkMan { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        public string LinkPhone { get; set; }

        /// <summary>
        /// 摄像机名称
        /// </summary>
        public string CameraName { get; set; }

        /// <summary>
        /// 登陆名
        /// </summary>
        public string CameraLoginName { get; set; }

        /// <summary>
        /// 登陆密码（明文）
        /// </summary>
        public string CameraLoginPwd { get; set; }

        /// <summary>
        /// 说明
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }
        #endregion

        #region 数据查询
        public static DataSet GetGridData(int pageIndex, int pageSize, string branchId, string cameraName)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };
            StringBuilder sbsql = new StringBuilder();
            sbsql.Append("SELECT SQL_CALC_FOUND_ROWS data.* from tblpartyvideoprobe data where 1=1");
            if (!string.IsNullOrEmpty(branchId))
            {
                sbsql.Append(" and data.BranchID=?BranchID ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "BranchID",
                        Value = branchId
                    });
            }
            if (!string.IsNullOrEmpty(cameraName))
            {
                sbsql.Append(" and data.cameraName like ?CameraName ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "CameraName",
                        Value = string.Format("%{0}%", cameraName)
                    });
            }
            sbsql.Append(" order by data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            DataSet ds = MySqlHelper.ExecuteDataSet(sbsql.ToString(), parameters.ToArray());
            return ds;
        }

        public static DataTable GetCameraAddress(string branchId, string ipType)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            string tempAddress = string.Empty;
            string tempPort = string.Empty;
            ipType = IpHelper.IpHelperMain(ipType);
            
            //1用于占位符
            ipType = "1" + ipType;
            
            if (ipType.IndexOf("移动") > 0)
            {
                tempAddress = "AddressMobile";
                tempPort = "PortMobile";

            }
            else if (ipType.IndexOf("联通") > 0)
            {
                tempAddress = "AddressUnicon";
                tempPort = "PortUnicon";
            }
            else
            {
                //上面2种都不存在，就都连接电信线路
                tempAddress = "AddressTelecom";
                tempPort = "PortTelecom";
            }

            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat(@"SELECT ID,BranchID,{0} as Address,{1} as`Port`,LinkMan,LinkPhone,CameraName,CameraLoginName,CameraLoginPwd,
                                Description, CreateTime, CreateBy, ModifyTime, ModifyBy
                                 from tblpartyvideoprobe where 1=1",tempAddress,tempPort);
            if (!string.IsNullOrEmpty(branchId))
            {
                sbsql.Append(" and ID=?ID ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "ID",
                        Value = branchId
                    });
            }
            return MySqlHelper.ExecuteDataTable(sbsql.ToString(), parameters.ToArray());
        }

        /// <summary>
        /// 是否有权限查看
        /// </summary>
        /// <param name="LoginID"></param>
        /// <returns></returns>
        public static bool IsHavePower(string LoginID)
        {
            string sql = " SELECT * FROM tblroleusers WHERE RoleID='479d2939736a4bf983117c9541fe3abd' AND LoginID='" + LoginID + "'";
            DataTable dt = MySqlHelper.ExecuteDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else return false;
        }
        #endregion

        public struct IpInfo
        {
            public string IpAddress { get; set; }
        }
    }
}
